home *** CD-ROM | disk | FTP | other *** search
/ Hyper Stacks 1994 May / Hyper Stacks (Pacific HiTech)(1994)[Mac].iso / HyperTalk / AppleTalk Toolkit v.2.5 / MISC / XCMDUtils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-02-22  |  6.7 KB  |  278 lines  |  [TEXT/MPS ]

  1. /*******************************************************************\
  2. *    file:         XCMDUtils.c                                            *
  3. *    version:    1.05ß                                                *
  4. *                                                                     *
  5. * Support routines for the Appletalk/Hypercard interface.            *
  6. * -----------------------------------------------------------------    *
  7. * By:    Greg Kimberly, Donald Koscheka                                *
  8. * Date:    21-Sept-87                                                    *
  9. * ©    Copyright 1987, Apple Computer, Inc.                            *
  10. *    All Rights Reserved                                                *
  11. *                                                                    *
  12. * -----------------------------------------------------------------    *
  13. *                        Modification History                        *
  14. * -----------------------------------------------------------------    *
  15. *  Date           | By    |                     Description                    *
  16. * -----------------------------------------------------------------    *
  17. * 21-Sep-87    | GK    | file created                                    *
  18. *  5-Nov-87    | DK    | changed ErrorReturn to return a handle to an    *
  19. *            |         | an error string rather than report the error    *
  20. *            |        | deliteralized default error number            *
  21. * 11-Dec-87    | DK    | changed save and retrieve handle to use longs    *
  22. * 14-Jan-88    | DK    | Retrieve and savehandles to pass the id of the*
  23. *            |        | resource to access.                            *
  24. *  1-Feb-88    | DK    | Return empty rather than "No Error" if no     *
  25. *            |         | error (a la James Redfern).                    *
  26. * 10-Feb-88    | DK    | changed Savehandle to set handle to empty    if     *
  27. *            |        | it receives a nil handle (rather than save 0)    *
  28. * 22-Feb-88 | DK    | Move all locked handles high                    *
  29. * -----------------------------------------------------------------    *
  30. \*******************************************************************/
  31.  
  32. #include    <Types.h>
  33. #include     <AppleTalk.h>
  34. #include    <Resources.h>
  35. #include     <OSUtils.h>
  36. #include    <Packages.h>
  37. #include    <Memory.h>
  38. #include    "atalkxcmd.h"
  39. #include    "xcmdutils.h"
  40. #include     "HyperXCmd.h"
  41.  
  42.  
  43. #define isUpper( c ) ( (c >= 'A' && c <= 'Z') ? 1 : 0 )
  44. #define isLower( c ) ( (c >= 'a' && c <= 'z') ? 1 : 0 )
  45. #define    toUpper( c ) ( (isLower( c )) ? c - 'a' + 'A': c )
  46. #define toLower( c ) ( (isUpper( c )) ? c + 'a' - 'A': c )
  47.  
  48.  
  49. void SaveHandle(paramPtr, theID, theHandle)
  50.     XCmdBlockPtr    paramPtr;
  51.     short            theID;
  52.     long             theHandle;
  53. /**********************************
  54. * Save a handle away in the Hypercard
  55. * global heap.  The name of the global 
  56. * variable to use is stored in the 
  57. * resource file.  Extract that name,
  58. * get a handle to the data and do the
  59. * dirty deed.
  60. *
  61. * In:    theID = resource id of the container
  62. *        theHandle = thehandle or pointer to save away
  63. *
  64. **********************************/
  65. {
  66.     Handle     name, DataHandle;
  67.     char     buffHandle[256];
  68.  
  69.     name = GetResource('STR ', theID );            /*get name of global*/
  70.     *buffHandle = '\0';
  71.     
  72.     if( theHandle )
  73.         LongToStr( paramPtr, theHandle, buffHandle );
  74.         
  75.     DataHandle = PasToZero(paramPtr, buffHandle);
  76.     MoveHHi( name );
  77.     HLock(name);
  78.     SetGlobal(paramPtr, *name, DataHandle );    /* put it into HyperCard global name */
  79.     DisposHandle( DataHandle );
  80.     HUnlock( name );
  81. }
  82.  
  83.  
  84. long RetrieveHandle(paramPtr, theID)
  85.     XCmdBlockPtr    paramPtr;
  86.     short            theID;
  87. /**********************************
  88. * Retrieve a handle away from the Hypercard
  89. * global heap.  The name of the global 
  90. * variable to use is stored in the 
  91. * resource file.  Extract that name,
  92. * get a handle to the data and 
  93. * return the data in that container.
  94. * note that hypercard stores all of
  95. * its data as null-strings, so we must
  96. * convert it back to a handle...
  97. *
  98. * In:    theID = id of the handle or pointer
  99. *                to retrieve.
  100. *
  101. **********************************/
  102. {
  103.     Handle        name;
  104.     Handle        DataHandle;
  105.     char        localString[256];
  106.     long        returnHandle;
  107.     
  108.     name = GetResource('STR ', theID );         /* get name of global    */
  109.     MoveHHi( name );
  110.     HLock(name);                                /* lock name            */
  111.     DataHandle = GetGlobal(paramPtr,*name);     /* get HyperCard global */
  112.     HUnlock(name);        
  113.     if ( DataHandle ){
  114.         MoveHHi( DataHandle );
  115.         HLock( DataHandle );
  116.         ZeroToPas(paramPtr, *DataHandle, localString);
  117.         returnHandle = StrToLong( paramPtr, localString );
  118.         HUnlock( DataHandle );
  119.         DisposHandle( DataHandle );
  120.         return( (long)returnHandle );
  121.     }
  122.     else
  123.         return( 0L );
  124. }
  125.  
  126.  
  127. Handle ErrorReturn( theErr )
  128.     short theErr;
  129. /**********************************
  130. * Given a system error number in theErr
  131. * extract its string from the resource
  132. * fork and returns it to HyperCard
  133. *
  134. * If we can't work out the error string,
  135. * build one using the error number.
  136. **********************************/
  137. {
  138.     Handle    strhand = nil;
  139.     char    s[256];
  140.     char    num[256];
  141.     long    len = 0;
  142.     
  143.     if(theErr == noErr)
  144.         return( nil );
  145.  
  146.     *s = '\0';
  147.     
  148.     strhand = GetResource( 'STR ', theErr );
  149.     if( strhand ){
  150.         /*** copy the string to another handle and save it off ***/
  151.         len = GetHandleSize( strhand );
  152.         BlockMove( *strhand, s, len );
  153.         p2cstr( s );
  154.     }
  155.     else{
  156.         s[0]='E'; s[1]='R'; s[2]='R'; s[3]='O'; s[4]='R'; s[5]=' '; s[6]='\0';
  157.         NumToString( (long)theErr, num );
  158.         sappend( s, num );
  159.         len = (long)slength( s );
  160.     }
  161.     
  162.     if( *s ){
  163.         strhand = NewHandle( len );
  164.         BlockMove( s, *strhand, len );
  165.     }
  166.     return( strhand );
  167. }
  168.  
  169.  
  170.  
  171. /*******************         Miscellaneous Support Routines        *******************/
  172.  
  173. void pStrCopy( inStr,outStr )
  174.     unsigned char *inStr, *outStr;
  175. /*********************************
  176. * copy the pascal-style string from
  177. * the input string to the output 
  178. * string.
  179. *********************************/
  180. {
  181.     short i;
  182.     
  183.     for (i = 0; i <= inStr[0]; i++)
  184.         outStr[i] = inStr[i];
  185. }
  186.  
  187.  
  188. short slength( str )
  189.     unsigned char    *str;
  190. /*********************************
  191. * return the length of a c string
  192. * including the '\0' byte. 
  193. *********************************/
  194. {
  195.     short i = 0;
  196.     
  197.     while( *str++ )
  198.         i++;
  199.         
  200.     return( (i+1) );
  201. }
  202.  
  203.  
  204. short strcopy( dst, src )
  205.     unsigned char    *dst, *src;
  206. /*********************************
  207. * Copy a "C" string to another
  208. * "C" string. 
  209. *********************************/
  210. {
  211.     while( *dst++ = *src++ );
  212. }
  213.  
  214.  
  215. short sappend( dst, src )
  216.     unsigned char     *dst, *src;
  217. /*********************************
  218. * NOTE: the destination
  219. * string must be large enough to hold
  220. * both strings going in!
  221. *
  222. * tacks source onto destination.
  223. *********************************/
  224. {
  225.     while( *dst )
  226.         dst++;
  227.     while( *dst++ = *src++ );
  228. }
  229.  
  230.  
  231. short strCMP( s1, s2 )
  232.     unsigned char    *s1, *s2;
  233. /*********************************
  234. * compare two c strings regardless
  235. * of case returning:
  236. *
  237. * <0 if s1 < s2
  238. * 0  if s1 == s2
  239. * >0 if s1 > s2
  240. *********************************/
  241. {
  242.     
  243.     for( ; toUpper( *s1 ) == toUpper( *s2 ); s1++, s2++ )
  244.         if( *s1 == '\0' )
  245.             return( 0 );
  246.             
  247.     return( *s1 - *s2 );
  248. }
  249.  
  250.  
  251. MakeAnswer( bool, str )
  252.     short    bool;
  253.     char    *str;
  254. /*********************
  255. * create the returing string
  256. * as "TRUE" or "FALSE"
  257. *********************/
  258. {
  259.     if( bool ){
  260.         *str++ = 'T';
  261.         *str++ = 'R';
  262.         *str++ = 'U';
  263.         *str++ = 'E';
  264.         *str++ = '\0';
  265.     }
  266.     else{
  267.         *str++ = 'F';
  268.         *str++ = 'A';
  269.         *str++ = 'L';
  270.         *str++ = 'S';
  271.         *str++ = 'E';
  272.         *str++ = '\0';
  273.     }
  274. }
  275.  
  276. /******************** C routines for HyperCard callbacks *************************/
  277. #include "XCmdGlue.inc.c"
  278.